package com.ftg.learn.chapter12;

import java.nio.charset.Charset;

/**
 * @author KangJx
 * @date 2021/7/20
 */
public class LearnString {

    public void learnString(){
//     定义
//        【类型】 【变量名称】= 【new】 【构造方法()】;
        String s = new String();

        byte[] bs = new byte[]{97,98,78,120};
        String s1 = new String(bs);
        System.out.println(s1);
        //gbk gb2312 utf-8(万国码)
        byte[] bs1 = new byte[]{97,98,99,120};
        String s2 = new String(bs1, Charset.forName("gbk"));

        String s3 = new String("中国人abc");

        System.out.println(s3.length()+"长度");

        //默认utf-8
        System.out.println(s3.getBytes().length);
        System.out.println(s3.getBytes(Charset.forName("utf-8")).length);
        System.out.println("abc".compareTo("def"));
    }

    public void learnEquals(){
//        == // 字符串是否指向同一地址
//        .equals();//
        String s1 = "abc";
        String s2 = "abc";
        String s3 = new String("abc");
        String s4 = new String("abc");

        String s5 = s3;

        String s6 = s1;
        String s7 ="Abc";

        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s7));
        System.out.println(s1.equalsIgnoreCase(s7));

        System.out.println(s2 == s3);
        System.out.println(s3 == s4);
        System.out.println(s3.equalsIgnoreCase(s4));
        System.out.println(s3.equals(s4));

        System.out.println(s5 == s1);
        System.out.println(s5 == s6);

        System.out.println(s5.equalsIgnoreCase("123"));
        //1、共有多少"序列"二字
        //2、打印出第一个序列出现的位置
        //3、给字一个字符串"2021-7-20",哪年，哪月,哪日
        String date = "2021-7-20";

        String[] ds = date.split("-");
        System.out.println(ds[0]);
        System.out.println(ds[1]);
        System.out.println(ds[2]);

        String year = date.substring(0,4);
        System.out.println("年" + year);
        String month = date.substring(date.indexOf("-")+1,date.lastIndexOf("-"));
        System.out.println(month);
        String day = date.substring(date.lastIndexOf("-")+1);
        System.out.println(day);

        //4、"abcdefghijk","acf"
        String str = "返回一个字符序列，该序列是该序列的子序列。返回一个字符序列，该序列是该序列的子序列。返回一个字符序列，该序列是该序列的子序列。返回一个字符序列，该序列是该序列的子序列。返回一个字符序列，该序列是该序列的子序列。返回一个字符序列，该序列是该序列的子序列。";
        int n = "abcabc".indexOf("ab");
        System.out.println(n);
        int m = "abcabc".indexOf("ab",n+"ab".length());
        System.out.println(m);
    }

    /**
     * tar里面每个字符是否都 在src出现过,true false
     * @param src  源串
     * @param tar  目标
     * @return
     * src : "abcdefg"
     * tar：“af”
     */
    public boolean myContains(String src,String tar){
        char[] cs = src.toCharArray();
        char[] ts = tar.toCharArray();

        int sum = 0;
        for(char t : ts){
            for(char c : cs){
               if(t == c){
                    sum++;
                    break;//跳出当前循环
               }
            }
        }

        return sum == tar.length();

    }

    /**
     * tar在src里面出现次数
     * @param src 源字符串
     * @param tar 目标
     * @return
     */
    public int findSubStrigCount(String src,String tar){
        if(null == src || src.length() == 0){
            return 0;
        }

        int sum = 0;
        for(int i = src.indexOf(tar); i != -1 ; i = src.indexOf(tar,i + 1)){
            sum++;
            System.out.println("第"+sum+"几个"+tar+"现现的位置是:" + i+"\t");
        }
        return sum;
    }



}
